package com.walle.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangjiapeng
 * @Package com.walle.netty
 * @Description: ${todo}
 * @date 2019/1/21 16:38
 */
public class TimeServerHandle extends SimpleChannelInboundHandler<String> {


    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String msg) throws Exception {
        System.out.println("received msg from client : " + msg);
        String rtn = sdf.format(new Date());
        channelHandlerContext.writeAndFlush(rtn);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
